/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.file.storage.tfs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.foreveross.bsl.file.storage.FileStorage;
import com.google.common.io.Files;
import com.taobao.common.tfs.DefaultTfsManager;
import com.taobao.common.tfs.packet.FileInfo;

/**
 * xxx
 *
 * @author Wangyi
 * @version v1.0
 *
 * @date 2013-9-12
 *
 */
public class TfsFileStorage implements FileStorage {
	
	private DefaultTfsManager tfsManager;
	
	/**
	 * 
	 */
	public TfsFileStorage(DefaultTfsManager tfsManager) {
		this.tfsManager = tfsManager;
	}
	
	public String saveFile(File file) {
    	if(file.isFile()){
            FileOutputStream output;
            byte[] data = new byte[1024];
			try {
				output = new FileOutputStream(file);
	    		int s = (int)file.length();
	    		data = new byte[s];
				output.write(data);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
    		return tfsManager.saveFile(data, file.getName(), null);
    	} else {
    		throw new RuntimeException("file cant not be a directory.");
    	}
    }
    
    public String saveFile(InputStream in) {
    	ByteArrayOutputStream bytestream = new ByteArrayOutputStream();  
    	try {
			int ch;  
			while ((ch = in.read()) != -1) {  
			    bytestream.write(ch);  
			}  
			byte data[] = bytestream.toByteArray();  
			return  tfsManager.saveFile(data, null, null);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}finally{
			try {
				in.close();
				bytestream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    }
    
    public String saveFile(String localFileName, String tfsFileName, String tfsSuffix) {
        return tfsManager.saveFile(localFileName, tfsFileName, tfsSuffix, false);
    }

    public String saveFile(String localFileName, String tfsFileName, String tfsSuffix, boolean simpleName) {
        return tfsManager.saveFile(localFileName, tfsFileName, tfsSuffix, simpleName);
    }

    public String saveFile(String tfsFileName, String tfsSuffix, byte[] data, int offset, int length) {
        return tfsManager.saveFile(tfsFileName, tfsSuffix, data, offset, length, false);
    }

    public String saveFile(String tfsFileName, String tfsSuffix,
                           byte[] data, int offset, int length, boolean simpleName) {
        return tfsManager.saveFile(tfsFileName, tfsSuffix, data, offset, length, simpleName);
    }

    public String saveFile(byte[] data, String tfsFileName, String tfsSuffix) {
        if (data != null) {
            return tfsManager.saveFile(tfsFileName, tfsSuffix, data, 0, data.length, false);
        }
        return null;
    }

    public String saveFile(byte[] data, String tfsFileName, String tfsSuffix, boolean simpleName) {
        if (data != null) {
            return tfsManager.saveFile(tfsFileName, tfsSuffix, data, 0, data.length, simpleName);
        }
        return null;
    }

    public String saveLargeFile(String localFileName, String tfsFileName, String tfsSuffix) {
        return tfsManager.saveLargeFile(localFileName, tfsFileName, tfsSuffix);
    }

    public String saveLargeFile(byte[] data, String tfsFileName, String tfsSuffix, String key) {
        if (data != null) {
            return tfsManager.saveLargeFile(tfsFileName, tfsSuffix, data, 0, data.length, key);
        }
        return null;
    }

    public String saveLargeFile(String tfsFileName, String tfsSuffix,
                                byte[] data, int offset, int length, String key) {
        return tfsManager.saveLargeFile(tfsFileName, tfsSuffix, data, offset, length, key);
    }

    public FileInfo statFile(String tfsFileName, String tfsSuffix) {
        return tfsManager.statFile(tfsFileName, tfsSuffix);
    }
    
    public boolean unlinkFile(String tfsFileName, String tfsSuffix) {
        return tfsManager.unlinkFile(tfsFileName, tfsSuffix);
    }

    public boolean hideFile(String tfsFileName, String tfsSuffix, int option) {
        return tfsManager.hideFile(tfsFileName, tfsSuffix, option);
    }

    public boolean fetchFile(String tfsFileName, String tfsSuffix, String localFileName) {
    	try {
			Files.createParentDirs(new File(localFileName));
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
    	return tfsManager.fetchFile(tfsFileName, tfsSuffix, localFileName);
    }

    public boolean fetchFile(String tfsFileName, String tfsSuffix, OutputStream output) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix,output);
    }

    public boolean fetchFile(String tfsFileName, String tfsSuffix, long offset, OutputStream output) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix, offset, output);
    }

    public boolean fetchFile(String tfsFileName, String tfsSuffix, long offset, long length, OutputStream output) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix, offset, length, output);
    }

    public boolean fetchFileForce(String tfsFileName, String tfsSuffix, String localFileName) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix, localFileName);
    }

    public boolean fetchFileForce(String tfsFileName, String tfsSuffix, OutputStream output) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix, output);
    }

    public boolean fetchFileForce(String tfsFileName, String tfsSuffix, long offset, OutputStream output) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix, offset, output);
    }

    public boolean fetchFileForce(String tfsFileName, String tfsSuffix, long offset, long length, OutputStream output) {
        return tfsManager.fetchFile(tfsFileName, tfsSuffix, offset, length, output);
    }

    public int openWriteFile(String tfsFileName, String tfsSuffix, String key) {
        return tfsManager.openWriteFile(tfsFileName, tfsSuffix, key);
    }

    public int openReadFile(String tfsFileName, String tfsSuffix) {
        return tfsManager.openReadFile(tfsFileName, tfsSuffix);
    }

    public int readFile(int fd, byte[] data, int offset, int length) {
        return tfsManager.readFile(fd, data, offset, length);
    }

    public int readFile(int fd, long fileOffset, byte[] data, int offset, int length) {
        return tfsManager.readFile(fd, fileOffset, data, offset, length);
    }

    public int writeFile(int fd, byte[] data, int offset, int length) {
        return tfsManager.writeFile(fd, data, offset, length);
    }

    public String closeFile(int fd) {
        return tfsManager.closeFile(fd);
    }
    
    /* (non-Javadoc)
     * @see com.foreveross.common.file.storage.FileStorage#destroy()
     */
    @Override
    public void destroy() {
    	this.tfsManager.destroy();
    }
    
    /* (non-Javadoc)
     * @see com.foreveross.common.file.storage.FileStorage#exists(java.lang.String)
     */
    @Override
    public boolean exists(String storedFileName) {
    	throw new RuntimeException("not implement yet, fix me!");
    }
    
    /* (non-Javadoc)
     * @see com.foreveross.common.file.storage.FileStorage#delete(java.lang.String)
     */
    @Override
    public boolean delete(String storedFileName) {
    	throw new RuntimeException("not implement yet, fix me!");
    }
    
    /**
	 * @return the tfsManager
	 */
	public DefaultTfsManager getTfsManager() {
		return tfsManager;
	}

	/**
	 * @param tfsManager the tfsManager to set
	 */
	public void setTfsManager(DefaultTfsManager tfsManager) {
		this.tfsManager = tfsManager;
	}
}
